home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: String Arrays and string parsing
- Date: 20 Feb 96 22:36:13 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.824855773@rscernix>
- References: <31287278.789445@news.inforamp.net> <4gc7qc$dm7@maureen.teleport.com>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4gc7qc$dm7@maureen.teleport.com> GHouck <hksys@teleport.com> writes:
-
- >dsheuman@inforamp.net (Danny Heuman) wrote:
- >>I have day, month, and year as DDMMMYYYY and would like to parse it
- >>out in to DD, MMM, YYYY. I can parse the DD out by using
- >>strncpy(into1, from1, 2). I can parse the YYYY out by doing
- >>strcpy(into3, from1 + 5). How can I parse out the MMM? Can I use
- >>either of these functions that work above, strncpy or strcpy? If so,
- >>once parsed, do I have to attach an '\0' to the end of it to keep it
- >>as a character string?
- >>
- >Yes, you can use 'strncpy'. Yes, you have to add zero. Try the following:
- >
- > strncpy( month,(datestring+2),3 ); /* copy MMM from interior */
- > month[3] = '\000'; /* add null terminator */
-
- Or you can parse everything with a single sscanf call:
-
- sscanf(from1, "%2s%3s%4s", into1, into2, into3);
-
- sscanf will null-terminate all three strings for you.
-
- If some of your characters can be spaces (e.g. " 2JAN1988"), you have
- to replace %Ns by %N[^\n] (that is, assuming that no embedded newlines
- are allowed :-)
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-